package cmu.andersonlab.hardpropagate;

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;

import java.net.*;
import java.io.*;

public class hardpropagate {
    public static final short PORT = 27000;
    private JTextField session;

    public static void create_window() {
        JFrame frame = new JFrame();
        JLabel label = new JLabel(String.format("Accepting connections on port %d", PORT));
        label.setPreferredSize(new Dimension(300,50));
        label.setHorizontalAlignment(SwingConstants.CENTER);

        JButton button = new JButton("Start Interactive Session");
        button.setActionCommand("start");
        button.setPreferredSize(new Dimension(200,40));
        button.addActionListener(new ActionListener () {
                public void actionPerformed(ActionEvent e) {
                    session sesh = new session(null);
                    sesh.setup_window();
                }
            });

        //frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(label, BorderLayout.CENTER);
        frame.getContentPane().add(button, BorderLayout.PAGE_END);
        frame.pack();
        frame.setVisible(true);
        frame.setTitle("Model Server");
    }

    public static void main_loop () {
        ServerSocket sock;
        Socket client;
        try {
            sock = new ServerSocket(PORT);
            System.out.println("Accepting connections...");

            while ((client = sock.accept()) != null) {
                new Thread(new session(client)).start();
            }
        } catch (IOException e) {
            System.err.println("Some mess up " + e);
        }
    }

    public static void main (String[] args) {
        // avoid race conditions?
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
            create_window();
                }
            });
        main_loop();
    }
}
